/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ssp.strategy.agent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import ssp.agent.QualityRequirement;
import ssp.database.DatabaseManager;
import ssp.net.NWcomponent;
import ssp.service.FrontendServer;
import ssp.utility.tmp;
import ssp.utility.Pair;

/**
 * Agent_v1クラスのStrategyを実際に実現したクラス
 * @author yamazaki
 */
public class Agent_v1_Strategy_v1 extends Agent_v1_Strategy {
    
    @Override
    public List<FrontendServer> searchFrontendServer(QualityRequirement qualityRequiremant, NWcomponent belongingNWcomponent, int num) {
        List<FrontendServer> frontendServersFromNetworkMap = new ArrayList<FrontendServer>(DatabaseManager.getINSTANCE().getNetworkMap().getNearbyServer(belongingNWcomponent.getID(), num));
        List<Integer> frontednServersFromServiceMap = new ArrayList<Integer>();
        Map<Pair<Integer, Integer>, Double> chargeMap = new TreeMap<Pair<Integer, Integer>, Double>(new tmp(DatabaseManager.getINSTANCE().getServiceMap().getChargeTable()));
        chargeMap.putAll(DatabaseManager.getINSTANCE().getServiceMap().getChargeTable());
        for(Entry<Pair<Integer, Integer>, Double> entry : chargeMap.entrySet()) {
            if(qualityRequiremant.getTypeID() == entry.getKey().getSecond()) {
                boolean qosFlg = true;
                for(int i=0 ; i<qualityRequiremant.getServiceQualityList().size() ; i++) {
                    if(DatabaseManager.getINSTANCE().getServiceMap().getQosIndex(entry.getKey(), i) < qualityRequiremant.getServiceQualityList().get(i)) {
                        qosFlg = false;
                        break;
                    }
                }
                if(qosFlg) {
                    frontednServersFromServiceMap.add(entry.getKey().getFirst());
                }
            }
        }
        List<FrontendServer> searchedServer = new ArrayList<FrontendServer>();
        for(FrontendServer frontendServer : frontendServersFromNetworkMap) {
            if(frontednServersFromServiceMap.contains(frontendServer.getID())) {
                searchedServer.add(frontendServer);
            }
        }
        return searchedServer;
    }
    
    @Override
    public Pair<FrontendServer, List<NWcomponent>> searchPath(QualityRequirement qos, NWcomponent belongingNWcomponent, FrontendServer frontendServer) {
        //
        throw new RuntimeException("This Method has not yet Implemeted.");
    }
    
    @Override
    public boolean makeService(FrontendServer frontendServer, List<NWcomponent> nwComponents) {
        //
        throw new RuntimeException("This Method has not yet Implemeted.");
    }
    
}
